<template>
  <div class="store-detail-container">
    <!-- 店铺信息区域 -->
    <div class="store-header">
      <div class="store-info-wrapper">
        <div class="store-basic-info">
          <img
            :src="store?.imageUrl"
            :alt="store?.name"
            class="store-logo"
            @error="handleImageError"
          />
          <div class="store-info">
            <h1 class="store-name">{{ store?.name }}</h1>
            <div class="store-stats">
              <div class="stat-item">
                <span class="label">店铺名称</span>
                <span class="value">{{ store?.name }}</span>
              </div>
              <div class="stat-item">
                <span class="label">店主</span>
                <span class="value">{{ store?.userName }}</span>
              </div>
              <div class="stat-item">
                <span class="label">开店时间</span>
                <span class="value">{{ formatDate(store?.createdAt) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 商品列表区域 -->
    <div class="products-section">
      <a-spin :spinning="loading" tip="加载中...">
        <div class="product-grid">
          <a-card
            v-for="product in products"
            :key="product.id"
            :hoverable="true"
            class="product-card"
            @click="goToProductDetail(product.id)"
          >
            <template #cover>
              <div class="product-image-container">
                <img
                  :src="getImageUrl(product.imageUrl)"
                  :alt="product.name"
                  class="product-image"
                  @error="handleImageError"
                />
                <div class="image-overlay">
                  <EyeOutlined class="view-icon" />
                  <span>查看详情</span>
                </div>
              </div>
            </template>
            <a-card-meta>
              <template #title>
                <div class="product-name" :title="product.name">
                  {{ product.name }}
                </div>
              </template>
              <template #description>
                <div class="price-sales">
                  <div class="price">
                    <span class="price-symbol">¥</span>
                    <span class="price-value">{{
                      product.price?.toFixed(2)
                    }}</span>
                  </div>
                  <div class="sales">
                    <ShoppingOutlined style="margin-right: 4px" />
                    {{ product.salesCount || 0 }}
                  </div>
                </div>
              </template>
            </a-card-meta>
          </a-card>
        </div>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <a-pagination
            v-model:current="searchParams.current"
            :total="totalCount"
            :pageSize="searchParams.pageSize"
            show-quick-jumper
            show-size-changer
            :pageSizeOptions="['20', '40', '60', '80']"
            @change="handlePageChange"
            @showSizeChange="handleSizeChange"
          />
        </div>
      </a-spin>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { EyeOutlined, ShoppingOutlined } from "@ant-design/icons-vue";
import { listStoreProductsUsingPost } from "@/api/shangpinjiekou";
import { getStoreInfoUsingPost } from "@/api/storeController";
import type API from "@/api";

const route = useRoute();
const router = useRouter();
const loading = ref(false);
const store = ref<API.Store>();
const products = ref<API.ClothingProduct[]>([]);
const total = ref(0);
const totalCount = computed(() => total.value || 0);

// 搜索参数
const searchParams = reactive({
  current: 1,
  pageSize: 20,
  storeId: Number(route.params.id),
});

// 添加计算属性用于显示总数
const showTotal = (t: number) => `共 ${t} 条`;

// 加载店铺信息
const loadStoreInfo = async () => {
  try {
    const res = await getStoreInfoUsingPost({ storeId: searchParams.storeId });
    if (res.data?.code === 0 && res.data.data) {
      store.value = res.data.data;
    }
  } catch (error) {
    console.error("获取店铺信息失败:", error);
    message.error("获取店铺信息失败");
  }
};

// 加载商品列表
const loadProducts = async () => {
  loading.value = true;
  try {
    const params = {
      current: searchParams.current,
      pageSize: searchParams.pageSize,
      storeId: searchParams.storeId,
    };
    const res = await listStoreProductsUsingPost(params);
    if (res.data?.code === 0 && res.data.data) {
      products.value = res.data.data.records || [];
      total.value = res.data.data.total || 0;
    }
  } catch (error) {
    console.error("获取商品列表失败:", error);
    message.error("获取商品列表失败");
  } finally {
    loading.value = false;
  }
};

// 页码改变
const handlePageChange = (page: number, pageSize?: number) => {
  searchParams.current = page;
  if (pageSize) {
    searchParams.pageSize = pageSize;
  }
  loadProducts();
  window.scrollTo({ top: 0, behavior: "smooth" });
};

// 每页条数改变
const handleSizeChange = (_current: number, size: number) => {
  searchParams.current = 1;
  searchParams.pageSize = size;
  loadProducts();
};

// 跳转到商品详情
const goToProductDetail = (id: number) => {
  router.push({
    name: "ProductDetail",
    params: { id: id.toString() },
  });
};

// 格式化日期
const formatDate = (date: string | undefined) => {
  if (!date) return "";
  return new Date(date).toLocaleDateString("zh-CN", {
    year: "numeric",
    month: "long",
    day: "numeric",
  });
};

// 图片URL处理
const getImageUrl = (imageUrl: string | undefined) => {
  if (!imageUrl) {
    return "https://img.js.design/assets/img/64832e1d1b5cdf515c927925.png#2aa0fdda";
  }
  return imageUrl.startsWith("http")
    ? imageUrl
    : "https://img.js.design/assets/img/64832e1d1b5cdf515c927925.png#2aa0fdda";
};

// 图片加载失败处理
const handleImageError = (e: Event) => {
  const img = e.target as HTMLImageElement;
  img.src =
    "https://img.js.design/assets/img/64832e1d1b5cdf515c927925.png#2aa0fdda";
};

onMounted(() => {
  loadStoreInfo();
  loadProducts();
});
</script>

<style scoped>
.store-detail-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 40px;
}

.store-header {
  background: #fff;
  padding: 24px 0;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.store-info-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.store-basic-info {
  display: flex;
  align-items: center;
  gap: 24px;
}

.store-logo {
  width: 120px;
  height: 120px;
  border-radius: 8px;
  object-fit: cover;
}

.store-info {
  flex: 1;
}

.store-name {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0 0 16px;
}

.store-stats {
  display: flex;
  gap: 40px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-item .label {
  font-size: 12px;
  color: #999;
}

.stat-item .value {
  font-size: 14px;
  color: #333;
}

.products-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 20px;
  margin-bottom: 24px;
}

.product-card {
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: none;
}

.product-card:hover {
  transform: translateY(-6px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12);
}

.product-image-container {
  position: relative;
  width: 100%;
  padding-top: 100%;
  overflow: hidden;
}

.product-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s ease;
}

.product-card:hover .product-image {
  transform: scale(1.08);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  color: white;
}

.product-card:hover .image-overlay {
  opacity: 1;
}

.view-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

.product-name {
  font-size: 14px;
  color: #333;
  margin: 8px 0;
  line-height: 1.4;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.price-sales {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.price {
  color: #ff4400;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: baseline;
}

.price-symbol {
  font-size: 12px;
  margin-right: 2px;
}

.price-value {
  font-size: 20px;
}

.sales {
  color: #999;
  font-size: 12px;
  display: flex;
  align-items: center;
}

.pagination-wrapper {
  margin-top: 32px;
  display: flex;
  justify-content: center;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .product-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 992px) {
  .product-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .product-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
